package com.hmdp.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.service.IBlogService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private IBlogService blogService;
    private static final Logger logger = LoggerFactory.getLogger(BlogController.class);

    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {
        //传入一个博客是实体类
        UserDTO user = UserHolder.getUser();
        // 获取当前登录用户
        blog.setUserId(user.getId());
        //获取该博客的的用户id
        blogService.save(blog);
        // 保存探店博文
        return Result.ok(blog.getId());
        // 返回id
    }

    @PutMapping("/like/{id}")
    //点赞会触发
    public Result likeBlog(@PathVariable("id") Long id) {
//        传入一个博客id,用于对点赞和取消点赞进行操作
        return blogService.likeBlog(id);
    }

    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
//        进入主页会触发
        UserDTO user = UserHolder.getUser();
        // 获取登录用户
//        System.out.println("当前登录用户: " + user.getId());
//        当前登录用户: 1010
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据,比如博客数量之类的
        List<Blog> records = page.getRecords();
//        使用 getRecords() 获取当前页的数据，并将其封装成一个 List<Blog> 返回，进一步传递给前端，展示给用户
        //如:博客ID: 23, 标题: 是是是
//        博客ID: 24, 标题: 收拾收拾
//        博客ID: 26, 标题: 111
//        for (Blog blog : records) {
//            System.out.println("博客ID: " + blog.getId() + ", 标题: " + blog.getTitle());
//        }
        return Result.ok(records);
        //返回给前端
    }

    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        System.out.println("hot部分被执行了");
        return blogService.queryHotBlog(current);
    }
    @GetMapping("/{id}")
//    点赞会触发
    public Result queryBlogById(@PathVariable("id") Long id) {
        return blogService.queryBlogById(id);
    }
    @GetMapping("/likes/{id}")
    //点击博客页会触发
    //进入博客页后点赞会触发
//    传入页面id
    public Result queryBlogLikes(@PathVariable("id") Long id) {
        return blogService.queryBlogLikes(id);
    }
    @GetMapping("/of/user")
    public Result queryBlogByUserId(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                    @RequestParam("id") Long id) {
        Page<Blog> page=blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }
    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(@RequestParam("lastId") Long max, @RequestParam(value="offset",defaultValue ="0" ) Integer offset) {
        return blogService.queryBlogOfFollow(max,offset);
    }


}
